package com.mingle.iwalk;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class PictureEditorActivity extends Activity {

	private POIInfo mInfo;
	private ImageView mPhoto;
	private Button mDeleteBtn;
	private Button mRotateBtn;
	private boolean mbRotate;
	private boolean mbDelete;
	
	public final static int PIC_ACTION_REMOVE = 0;
	public final static int PIC_ACTION_ROTATE = 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.picture_editor_layout);
        
        Button exitBtn = (Button) findViewById(R.id.exit_btn);
        exitBtn.setOnClickListener(mExitListener);
        mPhoto = (ImageView) findViewById(R.id.pic_view);
        mDeleteBtn = (Button) findViewById(R.id.delete_btn);
        mRotateBtn = (Button) findViewById(R.id.rotate_btn);

        mbRotate = false;
        mbDelete = false;
        
		String path = getIntent().getStringExtra("PIC_PATH");
		if(path == null || path.length() == 0)
		{
			finish();
			return;
		}
		mInfo = new POIInfo();
		mInfo.picPath = path;
			
		mPhoto.setImageURI(Uri.parse(path));
		mDeleteBtn.setOnClickListener(mDeleteListener);
		mRotateBtn.setOnClickListener(mRotateListener);
			
	}
	
	private Context getContext()
	{
		return PictureEditorActivity.this;
	}

	private OnClickListener mExitListener = new OnClickListener()
    {

		@Override
		public void onClick(View v) {
			
			exit();
			finish();
		}
    	
    };
    
    private OnClickListener mDeleteListener = new OnClickListener()
    {

		@Override
		public void onClick(View v) {
			
			mbDelete = true;
			Intent i = getIntent().putExtra("ACTION_CODE", PIC_ACTION_REMOVE);
			setResult(RESULT_OK, i);
			finish();
		}
    	
    };
    
    private OnClickListener mRotateListener = new OnClickListener()
    {

		@Override
		public void onClick(View v) {
			
			Bitmap bitmap = BitmapFactory.decodeFile(mInfo.picPath);
	    	int w = bitmap.getWidth();
	    	int h = bitmap.getHeight();
	    	
	    	Matrix matrix = new Matrix();
	    	matrix.postRotate(90);
	    	
	    	Bitmap resizeBitmap = Bitmap.createBitmap(bitmap,0,0, w, h, matrix, false);
	    	bitmap.recycle();
	    	
	    	try {
	    		File f = new File(mInfo.picPath);
	    		FileOutputStream  out = new FileOutputStream(f);
				 if(resizeBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) 
                 {
                         out.flush();
                         out.close();
                 }
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				
				e.printStackTrace();
			}

	    	mPhoto.setImageBitmap(resizeBitmap);
	    	
	    	mbRotate = true;
		}
    	
		
    };

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			exit();
		}
		return super.onKeyDown(keyCode, event);
	}
    
	private void exit()
	{
		if(mbRotate)
		{
				Intent i = getIntent().putExtra("ACTION_CODE", PIC_ACTION_ROTATE);
				setResult(RESULT_OK, i);
		}
		finish();
	}
    
}
